/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import controller.admin.RightsManagedBean;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.*;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.context.FacesContext;
import model.*;

/**
 *
 * @author shinji
 */
@Named(value = "u_searchManagedBean")
@SessionScoped
public class SearchManagedBean implements Serializable {

    @EJB
    private Member1Facade member1Facade;
    private String whoFind;
    private String beginAge;
    private String endAge;
    private String selectState;
    private List<String> lstAge = new ArrayList<String>();
    private List<Member1> lstFind = new ArrayList<Member1>();

    public List<Member1> getLstFind() {
        return lstFind;
    }

    public void setLstFind(List<Member1> lstFind) {
        this.lstFind = lstFind;
    }

    public String getSelectState() {
        return selectState;
    }

    public void setSelectState(String selectState) {
        this.selectState = selectState;
    }

    public String getBeginAge() {
        return beginAge;
    }

    public void setBeginAge(String beginAge) {
        this.beginAge = beginAge;
    }

    public String getEndAge() {
        return endAge;
    }

    public void setEndAge(String endAge) {
        this.endAge = endAge;
    }

    public List<String> getLstAge() {
        return lstAge;
    }

    public void setLstAge(List<String> lstAge) {
        this.lstAge = lstAge;
    }

    public String getWhoFind() {
        return whoFind;
    }

    public void setWhoFind(String whoFind) {
        this.whoFind = whoFind;
    }

    /**
     * Creates a new instance of SearchManagedBean
     */
    public SearchManagedBean() {
        for (int i = 18; i < 101; i++) {
            if (i == 100) {
                lstAge.add(String.valueOf(i) + "+");
            } else {
                lstAge.add(String.valueOf(i));
            }
        }
    }

    public String search() {
        switch (whoFind) {
            case "1":
                if (find("womem")) {
                    return "show";
                }else
                    return "";

            case "2":
                if (find("mem")) {
                    return "show";
                }else
                    return "";

            case "3":
                if (find("women")) {
                    return "show";
                }else
                    return "";

            case "4":
                if (find("mem")) {
                    return "show";
                }else
                    return "";

            default:
                return "";
        }
    }

    private boolean find(String gender) {
        try {
            MemberManagedBean memberManagedBean = get_MemberManagedBean();
            lstFind.clear();
            
            for (Member1 m : this.member1Facade.findAll()) {
                int age = new Date().getYear() - m.getBirthday().getYear();
                
                if (this.member1Facade.isFriend(memberManagedBean.getMember(), m) == false 
                        && memberManagedBean.getMember().getId() != m.getId() 
                        && m.getIDState().getId() == Integer.valueOf(selectState)
                        && age >= Integer.valueOf(beginAge) && age <= Integer.valueOf(endAge))
                    lstFind.add(m);
            }

            whoFind = "";
            
            if (lstFind == null)
                return false;
            else 
                return true;
            
        } catch (Exception e) {
            return false;
        }
    }
    
    public String search_Nickname(){
        try {
            MemberManagedBean memberManagedBean = get_MemberManagedBean();
            lstFind.clear();
            
            for (Member1 m : this.member1Facade.findAll()) {
                if(this.member1Facade.isFriend(memberManagedBean.getMember(), m) == false 
                        && memberManagedBean.getMember().getId() != m.getId()
                        && m.getNickName().equals(whoFind))
                    lstFind.add(m);
            }
            
            if(!lstFind.isEmpty())
                return "show";
            else
                return "";
        } catch (Exception e) {
            return "";
        }
    }

    private MemberManagedBean get_MemberManagedBean() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        MemberManagedBean rmb = (MemberManagedBean) FacesContext.getCurrentInstance().getApplication()
                .getELResolver().getValue(elContext, null, "u_memberManagedBean");
        return rmb;
    }
}
